<script setup>
import { ref } from 'vue'
import loginApi from "../api/loginApi.js";
import {ElMessage} from "element-plus";
import router from "../router/index.js";
import {tokenStore} from "../store/index.js";

const loginForm = ref({
  phone: '',
  password: '',
  captcha: '',
  key:''
})

const captchaSrc = ref('') // 验证码图片地址，暂时为空
showCaptcha()
function showCaptcha(){
  loginApi.getCaptcha().then(resp=>{
    captchaSrc.value = resp.data
    loginForm.value.key=resp.msg
  })
}
const handleLogin = () => {
  loginApi.login(loginForm.value).then(resp=>{
    console.log(resp)
    if (resp.code===200){
      tokenStore().update(resp.msg)
      ElMessage.success("登录成功")
      router.push('/')
    }
    else {
      ElMessage.error(resp.msg)
      showCaptcha()
    }
  })
  // 这里可以添加登录逻辑
}

const refreshCaptcha = () => {
  // 这里可以添加刷新验证码的逻辑
  showCaptcha()
}
</script>

<template>
  <div class="login-container">
    <div class="login-box">
      <h2 class="login-title">用户登录</h2>
      <form @submit.prevent="handleLogin" class="login-form">
        <div class="form-group">
          <label for="username">账号</label>
          <input
            id="username"
            v-model="loginForm.phone"
            type="text"
            placeholder="请输入账号"
            class="form-input"
          />
        </div>

        <div class="form-group">
          <label for="password">密码</label>
          <input
            id="password"
            v-model="loginForm.password"
            type="password"
            placeholder="请输入密码"
            class="form-input"
          />
        </div>

        <div class="form-group captcha-group">
          <label for="captcha">验证码</label>
          <div class="captcha-wrapper">
            <input
              id="captcha"
              v-model="loginForm.captcha"
              type="text"
              placeholder="请输入验证码"
              class="form-input captcha-input"
            />
            <img
              :src="captchaSrc"
              alt="验证码"
              class="captcha-image"
              @click="refreshCaptcha"
            />
          </div>
        </div>
        <router-link to="/register">注册</router-link>
        <button type="submit" class="login-button">登录</button>
      </form>
    </div>
  </div>
</template>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background-image: url('../assets/img/ad.png'); /* 使用项目中的背景图片 */
  background-size: cover;
  background-position: center;
  padding: 20px;
}

.login-box {
  background-color: rgba(255, 255, 255, 0.9);
  border-radius: 10px;
  padding: 30px;
  width: 100%;
  max-width: 400px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.login-title {
  text-align: center;
  margin-bottom: 30px;
  color: #333;
  font-size: 24px;
}

.login-form {
  display: flex;
  flex-direction: column;
}

.form-group {
  margin-bottom: 20px;
}

.form-group label {
  display: block;
  margin-bottom: 8px;
  color: #555;
  font-weight: bold;
}

.form-input {
  width: 100%;
  padding: 12px;
  border: 1px solid #ddd;
  border-radius: 5px;
  font-size: 16px;
  box-sizing: border-box;
}

.form-input:focus {
  outline: none;
  border-color: #409eff;
  box-shadow: 0 0 5px rgba(64, 158, 255, 0.3);
}

.captcha-group {
  margin-bottom: 30px;
}

.captcha-wrapper {
  display: flex;
  align-items: center;
  gap: 10px;
}

.captcha-input {
  flex: 1;
}

.captcha-image {
  width: 100px;
  height: 40px;
  background-color: #f0f0f0;
  border: 1px solid #ddd;
  border-radius: 5px;
  cursor: pointer;
}

.login-button {
  background-color: #409eff;
  color: white;
  border: none;
  padding: 12px;
  border-radius: 5px;
  font-size: 16px;
  cursor: pointer;
  transition: background-color 0.3s;
}

.login-button:hover {
  background-color: #337ecc;
}

@media (max-width: 480px) {
  .login-box {
    padding: 10px;
  }

  .captcha-wrapper {
    flex-direction: column;
    align-items: stretch;
  }

  .captcha-image {
    width: 100%;
    max-width: 150px;
    margin-top: 10px;
    align-self: center;
  }
}
</style>